<template>
  <view class="box">
    <image
      src="@/static/images/icon_grzy_xc_shangchuantupian.png"
      mode=""
      class="create"
      v-show="type"
      @click="UploadBtn"
    ></image>
    <view class="album_manage" v-if="type">
      <view class="album_manage_top">
        <view class="album_manage_top_title">我的相册</view>
        <view class="album_manage_top_btn" v-if="flag" @click="manage"
          >管理</view
        >
        <view
          class="album_manage_top_btn album_manage_top_btn1"
          v-else
          @click="manage"
          >完成</view
        >
      </view>
      <view class="album_select" @click="show = true">
        <u-icon name="calendar" color="#EEEEEE" size="30"></u-icon>
        <view>{{ timeStr }}</view>
      </view>
    </view>
    <view class="album_box">
      <view
        class="album_mod"
        :class="item.is_tong == 0 ? 'album_mod_block' : ''"
        v-for="(item, index) in listv"
        :key="index"
      >
        <view class="time" :class="item.is_tong == 2 ? 'time1' : ''">
          <text class="moon" v-show="item.is_tong != 1">{{ item.date_m }}</text>
          <text class="year" v-show="item.is_tong != 1"
            >/{{ item.date_y }}</text
          >
        </view>
        <view class="album_view">
          <view class="album_view_item" @click="itemSel(item)">
            <image :src="item.file_url" class="img"></image>
            <image
              src="@/static/images/my/icon_grzy_xc_weixuanzhong.png"
              class="radio"
              v-show="item.flag == 0 && flag == 0"
            ></image>
            <image
              src="@/static/images/my/icon_grzy_xc_xuanzhong.png"
              class="radio"
              v-show="item.flag == 1 && flag == 0"
            ></image>
          </view>
        </view>
      </view>
    </view>

    <u-datetime-picker
      :show="show"
      v-model="value1"
      mode="year-month"
      title="选择日期"
      :formatter="formatter"
      @confirm="confirm"
      confirmColor="#55B877"
      @cancel="cancel"
    ></u-datetime-picker>

    <view class="bottom_view" v-show="!flag">
      <view class="bottom_view_left" @click="select">
        <image
          src="@/static/images/my/icon_grzy_quanxuan.png"
          v-if="selected"
        ></image>
        <image
          src="@/static/images/my/icon_grzy_xc_xuanzhong.png"
          v-else
        ></image>
        <view>全选</view>
      </view>
      <view class="bottom_view_btn" @click="delFn">删除</view>
    </view>

    <view class="empty_view" v-if="list && list.length == 0">
      <image
        class="item_left_clickable"
        src="../static/images/my/kong.png"
        mode=""
      ></image>
      <view >当前相册暂无图片~</view>
    </view>

    <u-modal
      :show="show1"
      title="确认删除"
      :content="content"
      confirmColor="#55B877"
      :closeOnClickOverlay="true"
      @confirm="confirm1"
      @close="close1"
      @cancel="cancel1"
      :showCancelButton="true"
    ></u-modal>

    <u-modal
      :show="show2"
      title="确认上传"
      :content="content1"
      confirmColor="#55B877"
      :closeOnClickOverlay="true"
      @confirm="confirm2"
      @close="close2"
      @cancel="cancel2"
      :showCancelButton="true"
    ></u-modal>
  </view>
</template>
  
  <script>
import helper from "@/common/helper.js";
import config from "@/config"; // 配置文件
import storage from "@/common/storage.js"; // 缓存封装
export default {
  props: {
    list: {
      type: Array,
      default: () => [],
    },
    type: {
      type: String,
      default: "",
    },
  },
  watch: {
    list: {
      deep: true, // 深度监听每一个属性值, 如果是对象可以深度监听
      immediate: true, // 进入页面后立即自动调用handler函数
      handler(newValue) {
        this.listv = newValue;
        if (this.listv) {
          for (let i = 0; i < this.listv.length; i++) {
            this.listv[i].flag = 0;
          }
        }

        // console.log(this.listv, "newList");
      },
    },
  },
  onLoad(options) {
    // this.type = options.type;
  },
  data() {
    return {
      show: false,
      show1: false,
      show2:false,
      value1: Number(new Date()),
      flag: 1,
      selected: true,
      listv: [],
      allSel: 0,
      content: "确定要删除当前选中的图片吗?",
      content1:'确认上传当前选择的图片吗？',
      timeStr: "请选择月份",
      file_id:[]
      // type: "",
    };
  },
  methods: {
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      if (type === "day") {
        return `${value}日`;
      }
      return value;
    },
    // 确定日期
    confirm(value) {
      this.show = false;
      const today =
        helper.djsFull(value.value).y + "-" + helper.djsFull(value.value).m;
      this.timeStr =
        helper.djsFull(value.value).y +
        "年" +
        helper.djsFull(value.value).m +
        "月";
      // console.log(today, "asdasdasdasdasdasdasdasdasd");
      this.$emit("updateDataXiangce", today);
    },
    // 确定删除
    confirm1() {
      // this.show1 = false;
      let arr = [];
      let num = 0;
      for (let i = 0; i < this.listv.length; i++) {
        if (this.listv[i].flag == 1) {
          arr.push(this.listv[i].id);
          num++;
        }
      }
      // console.log(arr, "数组");
      this.deletFn(arr.join(","));
    },
    // 确认上传
    confirm2(){
      this.xiangceAdd()
    },
    close2(){
      this.show1 = false;
    },
    cancel2() {
      this.show2 = false;
    },
    close1() {
      this.show1 = false;
    },
    cancel1() {
      this.show1 = false;
    },
    cancel() {
      this.show = false;
    },
    manage() {
      this.$forceUpdate();
      this.flag = !this.flag;
    },
    // 全选
    select() {
      this.selected = !this.selected;
      this.allSel = !this.allSel;
      if (this.allSel == 0) {
        for (let i = 0; i < this.listv.length; i++) {
          this.listv[i].flag = 0;
        }
      } else {
        for (let i = 0; i < this.list.length; i++) {
          this.listv[i].flag = 1;
        }
      }
    },
    // 删除按钮
    delFn() {
      let isTrue = true;
      for (let i = 0; i < this.listv.length; i++) {
        // console.log(this.listv[i].flag);
        if (this.listv[i].flag == 1) {
          isTrue = false;
        }
      }
      if (isTrue == true) {
        uni.$u.toast("您还未选择图片");
      } else {
        this.show1 = true;
      }
    },
    // 删除接口
    deletFn(str) {
      this.$http
        .userXiangce_delete({ id: str })
        .then((res) => {
          uni.$u.toast(res.message);
          this.show1 = false;
          setTimeout(() => {
            this.$emit("updateDataXiangce");
          }, 1000);
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
    itemSel(item) {
      // 预览
      if (this.flag != 0) {
        uni.navigateTo({
          url: `/components/preview?type=true&img=${item.file_url}`,
        });
        return;
      }
      // 单选
      this.$forceUpdate();
      item.flag = !item.flag;
      let all = true;
      for (let i = 0; i < this.listv.length; i++) {
        if (this.listv[i].flag == 0) {
          all = false;
        }
      }
      if (all) {
        this.selected = false;
      } else {
        this.selected = true;
      }
    },
    // 上传接口
    UploadApi(filePath){
      let that = this;
      let token = storage.get("token");
      return new Promise((resolve, reject) => {
        let a = uni.uploadFile({
          url: config.uni_app_web_api_url + "/api/upload/image", // 仅为示例，非真实的接口地址
          filePath: filePath,
          name: "file",
          formData: {
            user: "test",
          },
          header: {
            "Access-Token": token,
          },
          success: (res) => {
            // console.log(res,'上传成功')
            if(res.statusCode != 200){
              return uni.$u.toast('上传失败，请重新上传');
            }
            const data = JSON.parse(res.data);
            that.file_id.push(data.data.fileInfo.file_id);
            // console.log(that.file_id,'文件集合')
          },
        });
      });
    },

    // 上传图片按钮
    UploadBtn() {
      let that = this
      // 拉起相册或拍照功能
      uni.chooseImage({
        count: 6, //默认9
        sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ["album"], //从相册选择
        success: function (res) {
          // console.log(res.tempFilePaths);
          let arr = res.tempFilePaths
          for(let i=0;i<arr.length;i++){
            that.UploadApi(arr[i])
          }
          setTimeout(() => {
            that.show2 = true
          }, 1000);
          
        },
      });
    },

    // 相册-添加
    xiangceAdd(){
      // console.log(this.file_id)
      this.$http
        .userXiangce_add({ file_id: this.file_id.join(",") })
        .then((res) => {
          uni.$u.toast(res.message);
          this.show2 = false
          this.file_id = []
          setTimeout(() => {
            this.$emit("updateDataXiangce");
          }, 1000);
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    }
  },
};
</script>
  
  <style scoped lang="scss">
.box {
  padding: 40rpx 30rpx;
  .create {
    width: 124rpx;
    height: 124rpx;
    position: fixed;
    bottom: 0;
    right: 0;
    margin-bottom: 260rpx;
    margin-right: 30rpx;
    z-index: 10;
  }
  .album_manage {
    .album_manage_top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 30rpx;
      .album_manage_top_title {
        color: #adadad;
      }
      .album_manage_top_btn {
        color: #55b877;
      }
      .album_manage_top_btn1 {
        color: #eeeeee;
      }
    }
    .album_select {
      background: rgba(39, 39, 39, 0.5);
      border-radius: 8rpx;
      display: flex;
      align-items: center;
      padding: 26rpx 30rpx;
      margin: 24rpx 0;
      view {
        font-size: 30rpx;
        color: #adadad;
        margin-left: 16rpx;
      }
    }
  }
  .album_box {
  }

  .album_mod {
    margin-bottom: 10rpx;
    float: left;
    width: 33.33%;
    .time {
      margin-top: 32rpx;
      margin-bottom: 32rpx;
      height: 70rpx;
      .moon {
        font-size: 50rpx;
        color: #eeeeee;
      }
      .year {
        font-size: 32rpx;
        color: #adadad;
      }
    }
    .time1 {
      display: none;
    }
    .album_view {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      .album_view_item {
        position: relative;
        // margin-right: 12rpx;
        // margin-bottom: 12rpx;
        .radio {
          width: 40rpx;
          height: 40rpx;
          position: absolute;
          right: 0;
          top: 0;
          margin-top: 12rpx;
          margin-right: 12rpx;
        }
      }
      .img {
        width: 222rpx;
        height: 222rpx;
        position: relative;
      }
      .album_view_item:nth-child(3n) {
        margin-right: 0;
      }
    }
  }
  .album_mod_block {
    // margin-left: 100rpx;
    clear: both;
  }
  .bottom_view {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16rpx 30rpx;
    box-sizing: border-box;
    background: #272727;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 1000;
    .bottom_view_left {
      display: flex;
      align-items: center;
      image {
        width: 40rpx;
        height: 40rpx;
        position: relative;
        top: 2rpx;
      }
      view {
        color: #eeeeee;
        margin-left: 16rpx;
      }
    }
    .bottom_view_btn {
      width: 250rpx;
      height: 80rpx;
      background: #ee6060;
      border-radius: 48rpx;
      text-align: center;
      line-height: 80rpx;
      font-size: 30rpx;
      color: #eeeeee;
    }
  }
  .empty_view {
    margin-top: 100rpx;
    image {
      margin: 0 auto;
      width: 410rpx;
      height: 410rpx;
    }
    view {
      font-size: 30rpx;
      color: #d8d8d8;
      text-align: center;
    }
  }
}
</style>